package com.pro01.backend.mapper;

import com.pro01.backend.dto.CommentDTO;
import com.pro01.backend.pojo.Forum2Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface Forum2CommentMapper {

    // 发表评论
    @Insert("""
        INSERT INTO forum2_comments (post_id, user_id, content, parent_comment_id, status, like_count, created_at)
        VALUES (#{postId}, #{userId}, #{content}, #{parentCommentId}, 'ACTIVE', 0, NOW())
    """)
    int insertComment(Forum2Comment comment);

    // 评论数 +1
    @Update("""
        UPDATE forum2_posts
        SET comment_count = comment_count + 1
        WHERE id = #{postId}
    """)
    int incrementCommentCount(@Param("postId") Long postId);

    // 显示评论列表
    @Select("""
    SELECT
        c.id AS commentId,
        c.post_id,
        c.content,
        c.user_id AS userId,
        u.username,
        u.title AS titleTag,
        u.nickname_color AS nicknameColor,
        u.avatar_frame AS avatarFrame,
        c.parent_comment_id,
        c.like_count,
        c.status,
        c.created_at
    FROM forum2_comments c
    JOIN users u ON c.user_id = u.id
    WHERE c.post_id = #{postId} AND c.status = 'ACTIVE'
    ORDER BY c.created_at ASC
    LIMIT #{limit} OFFSET #{offset}
""")
    List<CommentDTO> selectCommentsByPostId(@Param("postId") Long postId,
                                            @Param("offset") int offset,
                                            @Param("limit") int limit);

    @Select("""
    SELECT COUNT(*) FROM forum2_comments
    WHERE post_id = #{postId} AND status = 'ACTIVE'
""")
    int countCommentsByPostId(@Param("postId") Long postId);

    // 删除评论
    @Update("""
    UPDATE forum2_comments
    SET status = 'DELETED'
    WHERE id = #{commentId}
""")
    int markCommentAsDeleted(@Param("commentId") Long commentId);

    @Update("""
    UPDATE forum2_comments
    SET status = 'DELETED'
    WHERE parent_comment_id = #{parentId}
""")
    int markChildCommentsAsDeleted(@Param("parentId") Long parentId);

    @Select("SELECT user_id, post_id FROM forum2_comments WHERE id = #{commentId}")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "post_id", property = "postId")
    })
    Forum2Comment getCommentOwnerAndPost(@Param("commentId") Long commentId);

    // 评论数 -1
    @Update("""
    UPDATE forum2_posts
    SET comment_count = comment_count - 1
    WHERE id = #{postId} AND comment_count > 0
""")
    int decrementCommentCount(@Param("postId") Long postId);

    // 批量减去评论数
    @Update("""
    UPDATE forum2_posts
    SET comment_count = GREATEST(comment_count - #{count}, 0)
    WHERE id = #{postId}
""")
    int decrementCommentCountBy(@Param("postId") Long postId, @Param("count") int count);

    // 隐藏/显示评论
    @Update("""
    UPDATE forum2_comments
    SET status = #{status}
    WHERE id = #{commentId}
""")
    int updateCommentStatus(@Param("commentId") Long commentId, @Param("status") String status);

    @Select("SELECT user_id FROM forum2_comments WHERE id = #{commentId}")
    Long getCommentOwnerId(@Param("commentId") Long commentId);

    // 批量修改子评论状态
    @Update("""
    UPDATE forum2_comments
    SET status = #{status}
    WHERE parent_comment_id = #{parentId} AND status != 'DELETED'
""")
    int updateChildCommentStatus(@Param("parentId") Long parentId,
                                 @Param("status") String status);


    /**
     * 接下来是管理员接口
     */
    // 查看所有评论
    @Select("""
    SELECT
        c.id AS commentId,
        c.post_id,
        c.content,
        c.user_id AS userId,
        u.username,
        u.title AS titleTag,
        u.nickname_color AS nicknameColor,
        u.avatar_frame AS avatarFrame,
        u.level,
        c.parent_comment_id,
        c.like_count,
        c.status,
        c.created_at
    FROM forum2_comments c
    JOIN users u ON c.user_id = u.id
    ORDER BY c.created_at DESC
    LIMIT #{limit} OFFSET #{offset}
""")
    List<CommentDTO> selectAllCommentsPaged(@Param("offset") int offset,
                                            @Param("limit") int limit);

    @Select("SELECT COUNT(*) FROM forum2_comments")
    int countAllComments();

}
